<?php
/**
 * 网店基本设置
 * 网店基本信息设置 
 * 商城模板设置
 */
class Site {
	protected $site, $site_description;
	
	/**
	 * 实例化基本信息类函数
	 */
	public function __construct() {
		$this->site = M ( 'site' );
		$this->store = M ( 'o2o_store' );
		$this->area = M ( 'area' );
		$this->site_description = M ( 'site_description' );
		$this->banner = M ( 'banner' );
	}
	
	/**
	 *
	 * @param array $msg
	 *        	= {'user_id'=>'','name'=>'','logo'=>'','status'=>'','info'=>'','title'=>'','keyword'=>'','email'=>'','qq'=>'','telephone'=>'','cellphone'=>''}
	 *        	保存店铺基本信息 第一次保存为创建 设置createtime 再次修改为更新 设置 updatetime
	 * @return 成功返回true，失败返回false
	 */
	public function setbasemsg($msg = array()) {
		if (! $msg)
			return false;
			// 获取当前时间
		$time = mktime ();
		$map ['user_id'] = $msg ['user_id'];
		$ret = $this->site->where ( $map )->field ( 'id' )->select ();
		if ($ret) { // 如果存在 update
			$id = $ret [0] ['id'];
			if (! empty ( $msg ['name'] ))
				$smap ['name'] = $msg ['name'];
			if (! empty ( $msg ['status'] ))
				$smap ['status'] = $msg ['status'];
			if (! empty ( $msg ['domain'] ))
				$smap ['domain'] = $msg ['domain'];
			if (! empty ( $msg ['times'] ))
				$smap ['times'] = $msg ['times'];
			if (! empty ( $msg ['link_url'] ))
				$smap ['link_url'] = $msg ['link_url'];
			if (! empty ( $msg ['logo'] ))
				$dmap ['logo'] = $msg ['logo'];
			if (! empty ( $msg ['title'] ))
				$dmap ['title'] = $msg ['title'];
			if (! empty ( $msg ['keyword'] ))
				$dmap ['keyword'] = $msg ['keyword'];
			if (! empty ( $msg ['email'] ))
				$dmap ['email'] = $msg ['email'];
			if (! empty ( $msg ['info'] ))
				$dmap ['info'] = $msg ['info'];
			if (! empty ( $msg ['qq'] ))
				$dmap ['qq'] = $msg ['qq'];
			if (! empty ( $msg ['telephone'] ))
				$dmap ['telephone'] = $msg ['telephone'];
			if (! empty ( $msg ['cellphone'] ))
				$dmap ['cellphone'] = $msg ['cellphone'];
			if (! empty ( $msg ['icp'] ))
				$dmap ['link_url_icp'] = $msg ['icp'];
			if (! empty ( $msg ['aliww'] ))
				$dmap ['aliww'] = $msg ['aliww'];
			$dmap ['updatetime'] = $time;
			$this->site_description->where ( 'site_id=' . $id )->save ( $dmap );
			$reid = $this->site->where ( 'id=' . $id )->save ( $smap );
			if ($reid >= 0) {
				return true;
			}
		} else { // 表示创建
		       // add site
			$smap ['user_id'] = $msg ['user_id'];
			$smap ['name'] = $msg ['name'];
			$smap ['status'] = $msg ['status'] ? $msg ['status'] : 1;
			$smap ['domain'] = empty ( $msg ['domain'] ) ? $msg ['user_id'] : $msg ['domain']; // 默认设置为用户id
			$smap ['times'] = empty ( $msg ['times'] ) ? 0 : $msg ['times']; // 默认0
			$smap ['link_url'] = $msg ['link_url'];
			$smap ['link_url_status'] = 1;
			$id = $this->site->add ( $smap ); // 添加信息site
			if (empty ( $id ))
				return false;
				
				// add site_description
			$dmap ['site_id'] = $id;
			$dmap ['logo'] = $msg ['logo'] ? $msg ['logo'] : '';
			$dmap ['title'] = $msg ['title'] ? $msg ['title'] : '';
			$dmap ['info'] = $msg ['info'] ? $msg ['info'] : '';
			$dmap ['keyword'] = $msg ['keyword'] ? $msg ['keyword'] : '';
			$dmap ['email'] = $msg ['email'] ? $msg ['email'] : '';
			$dmap ['qq'] = $msg ['qq'] ? $msg ['qq'] : '';
			$dmap ['telephone'] = $msg ['telephone'] ? $msg ['telephone'] : '';
			$dmap ['cellphone'] = $msg ['cellphone'] ? $msg ['cellphone'] : '';
			$dmap ['aliww'] = $msg ['aliww'] ? $msg ['aliww'] : '';
			$dmap ['link_url_icp'] = $msg ['icp'] ? $msg ['icp'] : '';
			$dmap ['createtime'] = $time;
			$dmap ['updatetime'] = $time;
			$res = $this->site_description->add ( $dmap );
			if ($res)
				return true;
		}
		return false;
	}
	
	/**
	 *
	 * @param $user_id 获取店铺基本信息        	
	 * @return array $msg
	 */
	public function getbasemsg($user_id) {
		if (! $user_id)
			return false;
			// 获取当前店铺基本信息
		$msg = $this->site->join ( ' as s left join site_description as sd on s.id = sd.site_id' )->where ( 'user_id=' . $user_id )->find ();
		return $msg;
	}
	
	/**
	 *
	 * @param
	 *        	查找是否有相同的域名
	 * @return array $msg
	 */
	public function domain_search($domain) {
		$map ['domain'] = $domain;
		// 查找是否存在相同的域名
		$res = $this->site->where ( $map )->select ();
		return $res;
	}
	
	/**
	 *
	 * @param array $param
	 *        	= {'pid'=>'','consignee'=>'','zipcode'=>'','tel'=>'','province'=>'','city'=>'','district'=>'','address'=>'}
	 *        	保存店铺门店信息 第一次保存为创建 ， 再次修改为更新
	 * @return 成功返回true，失败返回false
	 */
	public function setstoreaddr($param = array()) {
		if (! $param)
			return false;
		
		$ret = $this->store->where ( 'pid=' . $param ['pid'] )->find ();
		if ($ret) { // 如果存在 update
			$reid = $this->store->where ( 'id=' . $ret ['id'] )->save ( $param );
			if ($reid >= 0)
				return true;
		} else { // 表示创建
			
			$res = $this->store->add ( $param );
			if ($res)
				return true;
		}
		return false;
	}
	
	/**
	 *
	 * @param $user_id 获取店铺基本信息        	
	 * @return array $msg
	 */
	public function getstoremsg($user_id) {
		if (! $user_id)
			return false;
			// 获取当前店铺基本信息
		$msg = $this->store->where ( 'pid=' . $user_id )->find ();
		return $msg;
	}
	
	/**
	 *
	 * @param $parent_id 获取该用户
	 *        	提货地址 省	市 县
	 * @return 省数组
	 */
	public function get_area($parent_id) {
		$ret = array ();
		$provinces = $this->area->where ( 'parentid=' . $parent_id )->select ();
		return $provinces;
	}
	
	/**
	 *
	 * @param $site_id 获取该商户自定义幻灯片        	
	 * @return array
	 */
	public function get_banners($site_id, $status = '', $limit = '') {
		if (empty ( $site_id ))
			return '';
		$map ['site_id'] = $site_id;
		if ($status !== '') {
			$map ['status'] = $status;
		}
		
		if ($limit) {
			$banners = $this->banner->order ( 'orders' )->limit ( $limit )->where ( $map )->select ();
		} else {
			$banners = $this->banner->order ( 'orders' )->where ( $map )->select ();
		}
		foreach ( $banners as $key => $value ) {
			if (! file_exists ( $value ['picture'] )) {
				$banners [$key] ['picture'] = $value ['picture'];
			}
		}
		return $banners;
	}
	
	/**
	 * 获取站点的信息
	 * 站点名称、SEO信息
	 * 站点门店地址、区域信息
	 * 站点配送、税金、等
	 * @date 2013-6-3
	 * @time 下午8:20:26
	 */
	function siteinfo($host = '', $id = '') {
		$this->did = 'yungou';
		$host_ori = $host;
		$host = explode ( '.', $host );
		$dao = M ( "Site" );
		if ($host [0] != 'www') { // 是否二级域名
			if (count ( $host ) == 4) {
				$host ['0'] = $host ['1'];
			}
			$re = $dao->where ( array (
					'domain' => $host ['0'] 
			) )->find ();
			if ($re) {
				$yunsite = $dao->join ( 'LEFT JOIN site_description ON site_description.site_id = site.id' )->where ( array (
						'site.id' => $re ['id'] 
				) )->find ();
				$this->cid = $re ['user_id'];
				$this->did = $re ['user_id'];
				$yunsite ['link_url_icp'] = 'Copyright &copy; 2013  广州联酷网络科技有限公司  版权所有   备案号：粤ICP备10000449号-2';
			}
		}
		if ($this->did == 'yungou') { // 是否独立域名
			$re = $dao->where ( array (
					'link_url' => $host_ori 
			) )->find ();
			if ($re) {
				$yunsite = $dao->join ( 'LEFT JOIN site_description ON site_description.site_id = site.id' )->where ( array (
						'site.id' => $re ['id'] 
				) )->find ();
				
				$this->cid = $re ['user_id'];
				$this->did = $re ['user_id'];
			}
		}
		if ($this->did == 'yungou') { // 平台进入
			$this->cid = $_REQUEST ['cid'] ? $_REQUEST ['cid'] : $_REQUEST ['site_id'];
			if ($this->cid) { // 带有cid
				$yunsite = $dao->join ( 'LEFT JOIN site_description ON site_description.site_id = site.id' )->where ( array (
						'site.id' => $re ['id'] 
				) )->find ();
			}
			$yunsite ['title'] = '云购通讯产业联盟';
			$yunsite ['name'] = '云购通讯产业联盟';
			$yunsite ['home'] = true;
			$yunsite ['link_url_icp'] = 'Copyright &copy; 2013  广州联酷网络科技有限公司  版权所有   备案号：粤ICP备10000449号-2';
			if (MODULE_NAME == 'Cart' && ACTION_NAME == 'index')
				$yunsite ['logo'] = '/Public/v2/img/logo.gif';
		}
		if (! $yunsite) {
			// echo json_encode(array('status'=>0,'msg'=>'链接有错！'.$a));
			// die();
		}
		$yunsite ['cid'] = $this->cid;
		$yunsite ['did'] = $this->did;
		return $yunsite;
	}
}
